import { Component, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { myValidNull } from '@core/util/string-utils';
import { NzMessageService } from 'ng-zorro-antd';
import { CustomerService } from '@core/routes-services/customer.service';
import { CustomerViewComponent } from '../customer-view/customer-view.component';
import { AgentService } from '@core/routes-services/agent.service';
import { InteractionModalComponent } from '@shared/interaction-modal/interaction-modal.component';

@Component({
  selector: 'app-customer-list',
  templateUrl: './customer-list.component.html',
})
export class CustomerListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  agentId: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  allAgents: any [] = [];

  constructor(private message: NzMessageService,
              private customerService: CustomerService,
              private agentService: AgentService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initAllAgents();
    this.initPage();
  }

  initAllAgents() {
    this.isLoading = true;
    this.agentService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.allAgents = res.data.agents;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error);
    });
  }

  initPage() {
    this.isLoading = true;
    this.customerService.pagination(this.page, this.keyword, this.agentId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(CustomerViewComponent, { entity: bak }, 'lg', {
      nzTitle: '客户信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  auditingEntity = (entity, map, reason) => {
    if (myValidNull(reason)) {
      this.updateEntity(entity, map);
    } else {
      this.modalHelper.open(InteractionModalComponent, { remark: entity[reason] }, 630, {
        nzTitle: '备注说明',
      }).subscribe(result => {
        if (result) {
          console.log(result);
          map[reason] = result;
          this.updateEntity(entity, map);
        }
        console.log('关闭弹窗');
      }, (error) => {
        console.error('弹窗回调出错：', error);
      });
    }
  };
  updateEntity = (entity, map) => {
    this.customerService.updateEntity(entity.id, map).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success('操作成功');
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  };

}
